//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "common\fn_registerObject.sqf"
#include <tmf_constants.h>
#include <tmf_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;
private ["_object","_actions","_action","_action_type","_action_id","_parameters","_instance_id","_func_name","_func","_param_values","_val","_params","_key"
		,"_obj_params","_obj_initline"];
DEFAULT_PARAM(_object,0,objNull);
DEFAULT_PARAM(_action,1,[]);
if (typename _object != typename objNull) exitWith {ERROR_1("Parameter is not an object",_this);};
if (isnull _object) exitWith {ERROR_1("Parameter is not set",_this);};
if (typename _action != typename []) exitWith {ERROR_1("Invalid Action Parameter",_this);};
if (count _action <= 0) exitWith {ERROR_1("No Action Parameter",_this);};

_instance_id = [_object] call FFUNC(GetInstanceID);
TRACE_1("Registering Action",ARR_4(_instance_id,_object, typeof _object, _action));

[_object] spawn FFUNC(storeObject);

_action_id = [_action] call FFUNC(actionGetID);
_action_type = [_action] call FFUNC(actionGetType);
_parameters = [_action] call FFUNC(actionGetParams);
_params = [];
_params set[TMF_REGISTER_OBJECT, _object];
_params set[TMF_REGISTER_ACTION, _action];
_params set[TMF_REGISTER_INSTANCE, _instance_id];

_obj_params = [_action] call FFUNC(actionGetObjParams);
_obj_initline =	GETVAL(_obj_params,TMF_VEH_PARAMS_INIT,[]);
if (count _obj_initline > 0) then {
	if (count _obj_initline == 1) then {
		call compile (_obj_initline select 0);
	} else {
		if (!isnil (_obj_initline select 1)) then {
			call compile format["_func = %1",(_obj_initline select 1)];
			(_obj_initline select 0) call _func;
		};
	};
};

if (GVAR(SERVER)) then {
	_func_name = format["%1%2",QUOTE(FFUNC(serverSetup)),_action_type];
	if (!isnil _func_name) then {
		// The following value is used to filter the objects out of the Cleanup
		OSETVAR(_object,"TMF_MISSION_OBJECT",true);
		OSETVAR(_object,"MSO_GC_IGNORE",true);
		call compile format["_func = %1",_func_name];
		TRACE_1("Registering Object",ARR_4(_func_name,_object, typeof _object, _parameters));
		_params call _func;
	} else {
		DEBUG_1("Action doesn't exist",ARR_4(_func_name,_action_id,_action_type,_parameters));
	};
};
// No need to register client actions on a HC
if (GVAR(CLIENT) && !GVAR(HCCLIENT)) then {
	_func_name = format["%1%2",QUOTE(FFUNC(clientSetup)),_action_type];
	if (!isnil _func_name) then {
		OSETVAR(_object,"TMF_MISSION_OBJECT",true);
		OSETVAR(_object,"MSO_GC_IGNORE",true);
		call compile format["_func = %1",_func_name];
		if ((toUpper _action_type) != "REGISTER") then {
			if ((player distance _object) < 20) then { player reveal _object; };
		};
		TRACE_1("Registering Object",ARR_4(_func_name,_object, typeof _object, _parameters));
		_params call _func;
	} else {
		DEBUG_1("Action doesn't exist",ARR_4(_func_name,_action_id,_action_type,_parameters));
	};
};		

TRACE("End");
TMFPROFILERSTOP;